// export default function ({store, redirect, app: {$axios}})  {
//   // 数据访问前缀
//   $axios.defaults.baseURL = 'http://api.zlygu.com/api/';
// }
export default function({ app, redirect},inject) {
  // request interceptor
  let $axios = app.$axios;
  $axios.defaults.baseURL = 'http://39.106.106.86:8088/api/';
  $axios.interceptors.request.use(
    config => {
      // do something before request is sent
      return config
    },
    error => {
      // do something with request error
      return Promise.reject(error)
    }
  )
  $axios.onRequest(config => {
    // console.log('config',config)
  })

  // response interceptor
  $axios.interceptors.response.use(
    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
      return response
    },
    error => {
      console.log('err' + error) // for debug
      return Promise.reject(error)
    }
  )
   $axios.onError(error => {
    const code = parseInt(error.response && error.response.status)
    if (code === 400) {
      redirect('/404')
    } else if (code === 500) {
      redirect('/500')
    }
  })
  inject('apiGet',(url,params={})=>{
    return new Promise((resolve,reject) => {
      $axios.$get(url,{
        params:params
      })
        .then(response => {
          resolve(response);
        })
        .catch(err => {
          reject(err);
          let message = '请求失败！请检查网络';
          //错误返回
          // if(err.response)message=err.response.data.message;
        })
    })
  })
  inject('apiPost',(url,data = {})=>{
    return new Promise((resolve,reject) => {
      $axios.$post(url,data)
        .then(response => {
          resolve(response);
        },err => {
          reject(err);
          let message = '请求失败！请检查网络';
        })
    })
  })
  //请求头部
  inject(('apiconfig'),(params={})=>{
    return new Promise((resolve,reject) => {
      if(app.$config){
         resolve(app.$config);
      }else {
        $axios.$get('config',{
          params:params
        })
          .then(response => {
            let conifg = {
              header:{
                title:'',
                meta:[],
              },
              setting:{

              },
            }
            response.data.map(item=>{
              if(item.title=="title"){
                conifg.header.title = item.val;
              }else if(item.title=="keywords"){
                conifg.header.meta.push(
                  { hid: 'keywords', name: 'keywords', content:item.val}
                )
              } else if(item.title=="description"){
                conifg.header.meta.push(
                  { hid: 'description', name: 'description', content:item.val}
                )
              }else {
                conifg.setting[item.title]= item;
              }
            })
            app.$config = conifg;
            resolve(conifg);
          })
          .catch(err => {
            reject(err);
            let message = '请求失败！请检查网络';
            //错误返回
            // if(err.response)message=err.response.data.message;
          })
      }

    })
  })
  // app.$apiGet = function(url,params={}){
  //   return new Promise((resolve,reject) => {
  //     $axios.$get(url,{
  //       params:params
  //     })
  //       .then(response => {
  //         console.log('app',app)
  //         resolve(response);
  //       })
  //       .catch(err => {
  //         reject(err);
  //         let message = '请求失败！请检查网络';
  //         //错误返回
  //         // if(err.response)message=err.response.data.message;
  //       })
  //   })
  // }
  // app.$apiPost= function(url,data = {}){
  //   return new Promise((resolve,reject) => {
  //     axios.post(url,data)
  //       .then(response => {
  //         resolve(response);
  //       },err => {
  //         reject(err);
  //         let message = '请求失败！请检查网络';
  //       })
  //   })
  // }
}
